import Vue from 'vue'
import VueRouter from 'vue-router'

import routes from './routes'
Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

// 路由守卫.判断去出租的过程,未登录进行拦截
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.localStorage.getItem('TOKEN')
  if (to.path === '/rent/add' || to.path === '/rent') return tokenStr ? next() : next('/login')
  next()
})

export default router
